package com.fs.ibox.service.impl;

import com.fs.ibox.dao.CollectionDao;
import com.fs.ibox.dao.IdCollectionDao;
import com.fs.ibox.dao.UserDao;
import com.fs.ibox.pojo.User;
import com.fs.ibox.service.UserService;
import com.fs.ibox.util.BeanFactory;
import com.fs.ibox.vo.SaleVO;
import com.fs.ibox.vo.UserCollection;
import com.fs.ibox.vo.ShowcollVO;

import java.util.List;

public class UserServiceimpl implements UserService {
    @Override
    public User login(User user) {
        UserDao bean = BeanFactory.createBean(UserDao.class);
        return bean.querNameAndPasswprd(user);
    }

    @Override
    public int register(User user) {
        UserDao bean = BeanFactory.createBean(UserDao.class);

        return bean.inserUserNameAndPassword(user);
    }

    @Override
    public List<UserCollection> showColl(User user) {
        //在tb_idcollection中查询cname、count
        IdCollectionDao idCollDaobean = BeanFactory.createBean(IdCollectionDao.class);
        CollectionDao collDaobean = BeanFactory.createBean(CollectionDao.class);

        List<UserCollection> userCollections = idCollDaobean.queryUserCollection(user);
        if (userCollections != null){
            //对list进行去重，并计算重复数量
            for (int i = 0; i < userCollections.size(); i++) {
                Integer num = 1;    //定义计数器
                for (int j = 0; j < userCollections.size(); j++) {
                    if ( i != j && userCollections.get(i).getCname().equals(userCollections.get(j).getCname())){
                        userCollections.remove(j);
                        j--;
                        num++;
                    }
                }
                userCollections.get(i).setNum(num);
            }
            //得到藏品详情表的地址
//            for (int i = 0; i < userCollections.size(); i++) {
//                userCollections.get(i).setUimg(collDaobean.queryImg(userCollections.get(i).getCname())); ;
//            }
        }

        return userCollections;
    }

    @Override
    public ShowcollVO showCollInto(String username, String cname) {
        UserDao bean = BeanFactory.createBean(UserDao.class);
        return bean.queryUserCollectionDetails(username,cname);
    }
    @Override
    public Double queryMoney(String username) {
        UserDao bean = BeanFactory.createBean(UserDao.class);
        System.out.println(bean.queryMoneyByUser(username));
        return bean.queryMoneyByUser(username);
    }

    @Override
    public Double Trade(String username, String user, Double money) {
        UserDao bean = BeanFactory.createBean(UserDao.class);
        bean.earn(username, money);
        Double newMoney = bean.updateMoney(user, money);
        return newMoney;
    }

    @Override
    public int sellColl(String cname, String cid,double price) {
        UserDao bean = BeanFactory.createBean(UserDao.class);
        return bean.updateInIdCollectionCstate(cname,cid,price);
    }

    @Override
    public List<SaleVO> sale(String username) {
        IdCollectionDao bean = BeanFactory.createBean(IdCollectionDao.class);

        return bean.queryCnameAndCidAndPriceAndcimg(username);
    }

    @Override
    public int cancelsale(String cname, String cid) {
        IdCollectionDao bean = BeanFactory.createBean(IdCollectionDao.class);
        return bean.updatecstateByusernameAndCid(cname,cid);
    }

    @Override
    public int update(User user) {
        UserDao bean = BeanFactory.createBean(UserDao.class);
        return bean.update(user);
    }

    @Override
    public List<User> queryByusername(String username) {
        UserDao bean = BeanFactory.createBean(UserDao.class);
        return bean.queryByusername(username);
    }

    @Override
    public void DeductionsByUsername(String username, double price) {
        UserDao bean = BeanFactory.createBean(UserDao.class);
        bean.DeductionsByUsername(username,price);
    }
}
